﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Threading;

namespace Assign6
{
    public partial class FormMain : Form
    {
        WSDLReader wsdlrd;
        TreeNode rootNode;
        public FormMain()
        {
            InitializeComponent();
            rootNode = new TreeNode("WebService");
            treeView_display.Nodes.Add(rootNode);
            treeView_display.SelectedNode = rootNode;
        }



        private void Button_parse_Click(object sender, EventArgs e)
        {
            goBusy();
            comboBox_url.Items.Add(comboBox_url.Text);
            try
            {
                XmlTextReader wsdl = new XmlTextReader(comboBox_url.Text);
                Thread parseThread = new Thread(new ParameterizedThreadStart(parse));
                parseThread.Start(wsdl);
                parseThread.Join();
                addWebServiceTree();

            }
            catch (Exception exp)
            {
                MessageBox.Show(this, exp.Message, "Exception", MessageBoxButtons.OK);

            }
            finally
            {
                goFree();
            }
           
        }
        private void parse(object wsdl)
        {
            wsdlrd = new WSDLReader((XmlTextReader)wsdl);
           
        }
        private void goBusy()
        {
            label_busy.Text = "busy";
            label_busy.BackColor = Color.Red;

        }
        private void goFree()
        {
            label_busy.Text = "free";
            label_busy.BackColor = Color.LawnGreen;
        }

        private void addWebServiceTree()
        {
            if (wsdlrd != null)
            {
                foreach (WebMethodCollection webService in wsdlrd.webMethodCollections)
                {
                    TreeNode serviceTree = new TreeNode();
                    serviceTree =new TreeNode(String.Format("WebServiceName:{0}",webService.serviceName));
                    serviceTree.Nodes.Add(String.Format("+++Binding:{0}", webService.bindingName));
                    serviceTree.Nodes.Add(String.Format("+++PortType:{0}", webService.portTypeName));
                    serviceTree.Nodes.Add(String.Format("+++Endpoint:{0}", webService.webServiceEndpoint));
                    serviceTree.Nodes.AddRange(getWebMethodsTree(webService));
                    
                    rootNode.Nodes.Add(serviceTree);
                    treeView_display.SelectedNode = rootNode;
                    
                }
               // treeView_display.ExpandAll();
            }
        }
        private TreeNode[] getWebMethodsTree(WebMethodCollection service)
        {
            List<TreeNode> methodsTree = new List<TreeNode>();
            foreach (WebMethod method in service)
            {
                TreeNode methodTreeNode = new TreeNode(String.Format("Operation:{0}", method.Name));
                TreeNode inputTreeNode=new TreeNode(String.Format("Input:{0}",method.input));
                TreeNode outputTreeNode=new TreeNode(String.Format("Output:{0}",method.output));
                foreach (WebMethod.Parameter parameter in method.InputParameters)
                {   
                    TreeNode parameterTreeNode=new TreeNode(String.Format("{0}:{1}",parameter.Name,parameter.Type));
                    parameterTreeNode.Nodes.AddRange(getChildParameterTree(parameter));
                    inputTreeNode.Nodes.Add(parameterTreeNode);
                }
                foreach (WebMethod.Parameter parameter in method.OutputParameters)
                {
                    TreeNode parameterTreeNode = new TreeNode(String.Format("{0}:{1}", parameter.Name, parameter.Type));
                    parameterTreeNode.Nodes.AddRange(getChildParameterTree(parameter));
                    outputTreeNode.Nodes.Add(parameterTreeNode);
                }
                methodTreeNode.Nodes.Add(inputTreeNode);
                methodTreeNode.Nodes.Add(outputTreeNode);
                methodsTree.Add(methodTreeNode);
            }
            return methodsTree.ToArray();
        }

        private TreeNode[] getChildParameterTree(WebMethod.Parameter parameter)
        {
            List<TreeNode> childParameterTree = new List<TreeNode>();
            foreach (WebMethod.Parameter childParameter in parameter.elements)
            {
                TreeNode childParameterTreeNode = new TreeNode(String.Format("{0}:{1}",childParameter.Name,childParameter.Type));
                childParameterTreeNode.Nodes.AddRange(getChildParameterTree(childParameter));
                childParameterTree.Add(childParameterTreeNode);
            }
            return childParameterTree.ToArray();
        }
        private void deleteNode(TreeNode dead)
        {

            if(dead!=null)
            {
                foreach (TreeNode deadChild in dead.Nodes)
                {
                    deleteNode(deadChild);
                }
            
               dead.Remove();
            }
        }

        private void button_clear_Click(object sender, EventArgs e)
        {
            if (rootNode.Nodes.Count > 0)
            {
                while (rootNode.Nodes.Count > 0)
                {
                    foreach (TreeNode serviceNode in rootNode.Nodes)
                    {
                        deleteNode(serviceNode);
                    }
                }
            }
            else
            {
                comboBox_url.Items.Clear();
            }
            comboBox_url.Text = "";
        }
    }
}
